import { SpriteFrame } from "cc"
import { UserInfo } from "../common/UserInfo"
import { HeroConfig, ResName, ResType, TotemConfig, getResName, getTimerName, itemdata, nftdata, shopdata, soldierConfig } from "../settings/const"
import ResManager from "../common/ResManager"


/**奖励 */
export class Bonus {
    id: number = 0
    /**类型 */
    type: string
    /**类型名称 */
    typeName: string
    /**名称 */
    name: string
    /**数量 */
    num: number

    async getImg(): Promise<SpriteFrame> {
        try {
            let sf = await ResManager.instance.loadAssetByUrl<SpriteFrame>(`bonusImg/${this.id}/spriteFrame`)
            return sf
        } catch (error) {
            console.log(error)
        }
    }

    static createByObj(obj:any):Bonus{
        let bonus = new Bonus()
        for(let key in obj){
            bonus[key] = obj[key]
        }
        
        bonus.name = Bonus.ResCode(bonus.type, bonus.id)
        bonus.typeName = ResName[bonus.type]
        return bonus
    }

    /**
     * 解析奖励字符串
     * @param bonus 奖励的字符串形态
     */
    static getBonusObj(bonus: string): Bonus[] {
        let str = this.bonusArrayDesc(bonus)
        let a = bonus.split(';')
        let b = str.split('|')
        let list = []
        for (let i = 0; i < a.length; i++) {
            let ao = a[i].split(',')
            let bo = b[i].split(':')
            let obj = new Bonus()

            if (ao[0] == 'D') {
                obj.type = ao[0]
                obj.typeName = ResName[ao[0]]
                obj.id = 0
                obj.num = parseInt(ao[1])
                obj.name = bo[0]
            } else {
                obj.type = ao[0]
                obj.typeName = ResName[ao[0]]
                obj.id = parseInt(ao[1])
                obj.num = parseInt(ao[2])
                obj.name = bo[0]
            }


            list.push(obj)
        }
        return list
    }

    static bonusArrayDesc(bs, showNum = true) {
        if (!bs) {
            return '';
        }
        if (typeof bs == "string") {
            bs = bs.split(";").reduce((sofar, cur) => {
                let ls = cur.split(",");
                if (ls.length >= 3) {
                    sofar.push({
                        type: ls[0],
                        id: ls[1],
                        num: ls[2],
                    });
                } else {
                    sofar.push({
                        type: ls[0],
                        id: 0,
                        num: ls[1],
                    });
                }
                return sofar;
            }, []);
        } else {
            bs = bs || [];
        }

        if (!Array.isArray(bs)) {
            bs = [bs];
        }

        let desc = "";

        for (let b of bs) {
            if (!!desc) {
                desc += "|";
            }

            desc += this.ResCode(b.type, b.id);
            if (showNum) {
                desc += ":";
                desc += b.num;
            }
        }

        return desc;
    }


    /**
   * 将指定类型和编码翻译为文本
   * @param {*} _type     资源类型
   * @param {*} _id       资源编码
   * @returns
   */
    static ResCode(_type, _id) {
        _id = parseInt(_id || 0);

        let desc = "";
        switch (_type) {
            case "CS":
            case "A":
            case "C":
            case "D":
            case "M":
            case "G":
            case 1:
            case 2:
            case 5:
            case 8:
            case 9:
            case 7:
            case "1":
            case "2":
            case "5":
            case "8":
            case "9":
            case "7": {
                desc += ResName[_type];
                break;
            }
            case "S":
            case "1000":
            case 1000: {
                if (soldierConfig[_id]) {
                    if (_id <= 10) {
                        desc += soldierConfig[parseInt(_id) + UserInfo.instance.race * 10][0];
                    } else {
                        desc += soldierConfig[_id][0];
                    }
                } else {
                    desc += `未知兵种(${_id})`;
                }
                break;
            }
            case "2000":
            case 2000:
            case "AT": {
                desc += getTimerName(parseInt(_id));
                break;
            }
            case "3000":
            case 3000:
            case "R": {
                desc += getResName(parseInt(_id));
                break;
            }
            case "30000":
            case 30000:
            case "CA": {
                if (HeroConfig[_id]) {
                    desc += `${ResName[30000]}-${HeroConfig[_id].name}`;
                } else {
                    desc += `未知英雄(${_id})`;
                }
                break;
            }
            case "40000":
            case 40000:
            case "T": {
                if (TotemConfig[ResType.Totem + _id]) {
                    desc += TotemConfig[ResType.Totem + _id].name;
                } else {
                    desc += `未知图腾(${_id})`;
                }
                break;
            }
            case "60000":
            case 60000:
            case "N": {
                if (nftdata[60000 + _id]) {
                    desc += nftdata[60000 + _id].name;
                } else {
                    desc += `未知NFT(${_id})`;
                }
                break;
            }
            case "80000":
            case 80000:
            case "I": {
                if (itemdata[80000 + _id]) {
                    desc += itemdata[80000 + _id].desc;
                } else {
                    desc += `未知道具(${_id})`;
                }
                break;
            }
            case "90000":
            case 90000:
            case "B": {
                if (shopdata[_id]) {
                    desc += shopdata[_id].desc;
                } else {
                    desc += `未知礼包(${_id})`;
                }
                break;
            }
            default: {
                desc += _type + "." + _id;
                break;
            }
        }
        return desc;
    }
}